package com.smart.community.commons.constant;

/**
 * 缓存常量类
 * 定义缓存键命名规范、缓存区域名称和过期时间常量
 * 
 * @author Wu.Liang
 * @version 1.0.0
 * @since 2024-12-19
 */
public class CacheConstants {

    // ========================= 缓存区域名称 =========================
    /** 用户缓存区域 */
    public static final String USER_CACHE = "USER";
    
    /** 角色缓存区域 */
    public static final String ROLE_CACHE = "role";
    
    /** 权限缓存区域 */
    public static final String PERMISSION_CACHE = "permission";
    
    /** 社区缓存区域 */
    public static final String COMMUNITY_CACHE = "community";
    
    /** 楼栋缓存区域 */
    public static final String BUILDING_CACHE = "building";
    
    /** 物业缓存区域 */
    public static final String PROPERTY_CACHE = "property";
    
    /** 停车缓存区域 */
    public static final String PARKING_CACHE = "parking";
    
    /** 安防缓存区域 */
    public static final String SECURITY_CACHE = "security";
    
    /** 紧急求助缓存区域 */
    public static final String EMERGENCY_CACHE = "emergency";
    
    /** 通知缓存区域 */
    public static final String NOTIFICATION_CACHE = "notification";
    
    /** 系统配置缓存区域 */
    public static final String SYSTEM_CONFIG_CACHE = "system_config";
    
    /** 字典缓存区域 */
    public static final String DICT_CACHE = "dict";
    
    /** 通用实体缓存区域 */
    public static final String ENTITY_CACHE = "entity";

    // ========================= 缓存过期时间（秒） =========================
    /** 5分钟 */
    public static final long EXPIRE_5_MIN = 5 * 60;
    
    /** 10分钟 */
    public static final long EXPIRE_10_MIN = 10 * 60;
    
    /** 30分钟 */
    public static final long EXPIRE_30_MIN = 30 * 60;
    
    /** 1小时 */
    public static final long EXPIRE_1_HOUR = 60 * 60;
    
    /** 2小时 */
    public static final long EXPIRE_2_HOUR = 2 * 60 * 60;
    
    /** 6小时 */
    public static final long EXPIRE_6_HOUR = 6 * 60 * 60;
    
    /** 12小时 */
    public static final long EXPIRE_12_HOUR = 12 * 60 * 60;
    
    /** 1天 */
    public static final long EXPIRE_1_DAY = 24 * 60 * 60;

    // ========================= 缓存键前缀规范 =========================
    
    /** 用户相关缓存键前缀 */
    public static class UserKeys {
        public static final String USER_ID = "user:id:";
        public static final String USER_USERNAME = "user:username:";
        public static final String USER_PHONE = "user:phone:";
        public static final String USER_EMAIL = "user:email:";
        public static final String USER_ROLES = "user:roles:";
        public static final String USER_PERMISSIONS = "user:permissions:";
        public static final String USER_COMMUNITY = "user:community:";
    }
    
    /** 社区相关缓存键前缀 */
    public static class CommunityKeys {
        public static final String COMMUNITY_ID = "community:id:";
        public static final String COMMUNITY_CODE = "community:code:";
        public static final String COMMUNITY_NAME = "community:name:";
        public static final String COMMUNITY_TYPE = "community:type:";
        public static final String COMMUNITY_STATUS = "community:status:";
        public static final String COMMUNITY_REGION = "community:region:";
        public static final String COMMUNITY_MANAGER = "community:manager:";
        public static final String COMMUNITY_STATISTICS = "community:statistics:";
        public static final String COMMUNITY_ACTIVE = "community:active";
    }
    
    /** 物业相关缓存键前缀 */
    public static class PropertyKeys {
        public static final String FEE_ID = "property:fee:";
        public static final String BILL = "property:bill:";
        public static final String REPAIR_ORDER = "property:repair:";
        public static final String COMPLAINT = "property:complaint:";
        public static final String FACILITY = "property:facility:";
    }
    
    /** 停车相关缓存键前缀 */
    public static class ParkingKeys {
        public static final String SPACE_ID = "parking:space:";
        public static final String VEHICLE_ID = "parking:vehicle:";
        public static final String RECORD = "parking:record:";
        public static final String AVAILABLE_SPACES = "parking:available:";
        public static final String COMMUNITY_SPACES = "parking:community:spaces:";
    }
    
    /** 安防相关缓存键前缀 */
    public static class SecurityKeys {
        public static final String DEVICE_ID = "security:device:";
        public static final String ACCESS_RECORD = "security:access:";
        public static final String PATROL_RECORD = "security:patrol:";
        public static final String ALERT = "security:alert:";
        public static final String COMMUNITY_DEVICES = "security:community:devices:";
    }
    
    /** 紧急求助相关缓存键前缀 */
    public static class EmergencyKeys {
        public static final String CALL_ID = "emergency:call:";
        public static final String CONTACTS_USER = "emergency:contacts:";
        public static final String AVAILABLE_RESOURCES = "emergency:available_resources:";
        public static final String RESOURCES_TYPE = "emergency:resources:";
        public static final String PLANS_TYPE = "emergency:plans:";
        public static final String STATISTICS = "emergency:statistics:";
        public static final String PENDING = "emergency:pending";
        public static final String PROCESSING = "emergency:processing";
        public static final String COMPLETED = "emergency:completed";
    }
    
    /** 系统配置相关缓存键前缀 */
    public static class SystemKeys {
        public static final String CONFIG = "system:config:";
        public static final String DICT = "system:dict:";
        public static final String MENU = "system:menu:";
        public static final String PERMISSION = "system:permission:";
    }

    // ========================= 缓存监控配置 =========================
    /** 缓存命中率统计周期（分钟） */
    public static final int CACHE_HIT_RATE_PERIOD = 5;
    
    /** 缓存异常告警阈值（次数/分钟） */
    public static final int CACHE_ERROR_ALERT_THRESHOLD = 10;
    
    /** 缓存大小告警阈值（MB） */
    public static final long CACHE_SIZE_ALERT_THRESHOLD = 500;
    
    /** 缓存预热批次大小 */
    public static final int CACHE_WARM_UP_BATCH_SIZE = 100;
    
    /** 最大缓存重试次数 */
    public static final int MAX_CACHE_RETRY_COUNT = 3;

    // ========================= 分布式缓存配置 =========================
    /** 默认分布式锁超时时间（毫秒） */
    public static final long DEFAULT_LOCK_TIMEOUT = 30000;
    
    /** 默认分布式锁等待时间（毫秒） */
    public static final long DEFAULT_LOCK_WAIT_TIME = 5000;

    // ========================= 缓存一致性配置 =========================
    /** 缓存更新模式 */
    public enum CacheMode {
        /** 旁路缓存模式 */
        CACHE_ASIDE,
        /** 读写穿透模式 */
        READ_WRITE_THROUGH,
        /** 写后模式 */
        WRITE_BEHIND
    }
    
    /** 缓存失效策略 */
    public enum EvictionPolicy {
        /** 最近最少使用 */
        LRU,
        /** 最近最少频繁使用 */
        LFU,
        /** 先进先出 */
        FIFO,
        /** 定时失效 */
        TTL
    }

    // ========================= 缓存键生成器 =========================
    
    /**
     * 生成通用缓存键
     * 
     * @param module 模块名
     * @param type 键类型
     * @param value 键值
     * @return 缓存键
     */
    public static String buildKey(String module, String type, Object value) {
        return module + ":" + type + ":" + value;
    }
    
    /**
     * 生成用户缓存键
     * 
     * @param type 键类型
     * @param value 键值
     * @return 缓存键
     */
    public static String userKey(String type, Object value) {
        return UserKeys.class.getSimpleName().toLowerCase() + ":" + type + ":" + value;
    }
    
    /**
     * 生成社区缓存键
     * 
     * @param type 键类型
     * @param value 键值
     * @return 缓存键
     */
    public static String communityKey(String type, Object value) {
        return CommunityKeys.class.getSimpleName().toLowerCase() + ":" + type + ":" + value;
    }
    
    /**
     * 生成紧急求助缓存键
     * 
     * @param type 键类型
     * @param value 键值
     * @return 缓存键
     */
    public static String emergencyKey(String type, Object value) {
        return EmergencyKeys.class.getSimpleName().toLowerCase() + ":" + type + ":" + value;
    }

    // 工具类，防止实例化
    private CacheConstants() {
    }
} 